package com.longtop.framework.sys.user.web;

import java.util.HashMap;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.org.service.OrgService;
import com.longtop.framework.sys.team.model.TSysUserTeam;
import com.longtop.framework.sys.team.service.TeamService;
import com.longtop.framework.sys.user.service.UserService;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.intelliweb.view.Page;
import com.longtop.intelliweb.view.Panel;

/**
 * 
 * @author SJH
 * 
 */
public class UserDropdownAction extends BaseAction {

	private final String DATASET_USER = "Dataset_User";
	private final String DATASET_ORG = "Dataset_Org";
	private final String DATASET_TEAMUSER = "Dataset_TeamUser";

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		Dataset userDataset = this.getDataset(context, DATASET_USER);
		Dataset orgDataset = this.getDataset(context, DATASET_ORG);
		if (!WebUtil.validateDatasetExist(this, userDataset, DATASET_USER)) {
			return;
		}
		UserService userService = getBean(UserService.BEAN_ID);

		// 附加信息：判断是否来自WINDOW
		Page page = (Page) context.getControl("ePage");
		String tag = page.getTag();
		if ("window".equals(tag)) {// 弹出窗
			// 根据用户数据权限，设置默认机构值：权限控制
			userService.getOrgIdByAuth(context);
		} else {
			String showOrg = this.getParameter(context, "showOrg"); // 是否显示机构树
			if (!showOrg.equals("false")) {
				// 加载机构列表:权限控制
				OrgService orgService = getBean(OrgService.BEAN_ID);
				orgService.getOrgTreeAuth(orgDataset, context);
			} else {
				Panel panel = (Panel) context.getControl("orgTree");
				panel.setVisible("false");
				panel.setStyle("display:none");
			}

			// 加载群组：
			Dataset teamDataset = this.getDataset(context, DATASET_TEAMUSER);
			if (!WebUtil.validateDatasetExist(this, teamDataset, DATASET_TEAMUSER)) {
				return;
			}
			// 读取主表：权限控制
			TeamService teamService = this.getBean(TeamService.BEAN_ID);
			SessionObject sessionObject = this.getSessionObject(context);
			teamService.getTeamByAuth(sessionObject, teamDataset, TSysUserTeam.class);
		}

	}

	/**
	 * 每个业务模块公共查询方法，供自定义查询方法及翻页查询调用
	 * 
	 * @param dataset
	 * @param context
	 * @param customAction
	 */
	private void commonQuery(IActionContext context, boolean customAction) {
		Dataset userDataset = this.getDataset(context, DATASET_USER);

		if (customAction) {
			userDataset.setPageNo(1);
		}

		UserService userService = getBean(UserService.BEAN_ID);
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap = this.getElementsToMap(context);
		String orgId = this.getParameter(context, "orgId");
		queryMap.put("orgId", orgId);
		userService.queryForPageByIbatis("sysUserSql.selectUserToDropDown", null, queryMap, userDataset, false);
	}

	/**
	 * 翻页查询
	 * 
	 * @param context
	 */
	public void load(IActionContext context) {
		commonQuery(context, false);
	}

	/**
	 * 查询
	 * 
	 * @param context
	 */
	public void search(IActionContext context) {
		commonQuery(context, true);
	}

	/**
	 * 加载机构树(延迟加载)
	 * 
	 * @param context
	 */
	public void loadOrg(IActionContext context) {
		this.CommonLoadOrg(context);
	}

	/**
	 * 加载机构树
	 * 
	 * @param context
	 */
	private void CommonLoadOrg(Object context) {
		Dataset orgDataset = this.getDataset(context, DATASET_ORG);
		if (!WebUtil.validateDatasetExist(this, orgDataset, DATASET_ORG)) {
			return;
		}
		OrgService orgService = getBean(OrgService.BEAN_ID);

		QueryConditions queryConditions = WebUtil.createQueryConditions();
		String filter = orgDataset.getFilter();

		QueryResult queryResult = orgService.loadOrg(queryConditions, filter);
		try {
			BeanUtil.queryResultToDataset(queryResult, orgDataset, false, true);
		} catch (Exception e) {
			e.printStackTrace();
			hintErrorMessage("加载机构出错。");
		}

	}
}
